package com.yanqu.road.server.cmd.activity.suitang;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_SUI_TANG_AUTO_SETTING_INGOTS_OPERATE, desc = "自动设置元宝操作")
public class SuiTangAutoSettingIngotsOperateCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
            return;
        }
        SuiTang.SuiTangAutoSettingIngotsOperateReqMsg reqMsg = SuiTang.SuiTangAutoSettingIngotsOperateReqMsg.parseFrom(packet.getBytes());

        if (reqMsg.getOperateType() == 1) {
            // 增加元宝
            if (reqMsg.getOffsetIngotsTimes() <= 0) {
                ret = GameErrorCode.E_ERROR_PARAMETER;
                return;
            }
            Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(reqMsg.getOffsetIngotsTimes()).multiply(BigInteger.valueOf(config.getParamConfig().getAutoSettingIngotsNum())));
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
        }

        // 跨服执行操作
        SuiTang.CrossSuiTangAutoSettingIngotsOperateReqMsg.Builder builder = SuiTang.CrossSuiTangAutoSettingIngotsOperateReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SUI_TANG_AUTO_SETTING_INGOTS_OPERATE, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        SuiTang.SuiTangAutoSettingIngotsOperateRespMsg.Builder respMsg = SuiTang.SuiTangAutoSettingIngotsOperateRespMsg.newBuilder();
        player.sendPacket(ClientProtocol.U_SUI_TANG_AUTO_SETTING_INGOTS_OPERATE, respMsg);
    }
}
